<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-search" size="mini" @click="handleSearch">查询</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['device:assistDevice:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['device:assistDevice:edit']">
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['device:assistDevice:remove']">
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport('#assistDevice', '辅助设备')"
          v-hasPermi="['device:assistDevice:export']">
          导出
        </el-button>
      </el-col>

      <!-- 分页效果 -->
      <el-col :span="2.2" style="position: absolute; right: 0px; top: 0">
        <Page
          style="margin-right: 0px"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total"
          @on-change="pageChange"
          simple></Page>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      highlight-current-row
      border
      stripe
      v-loading="loading"
      id="assistDevice"
      :height="tableH"
      :data="assistDeviceList"
      @selection-change="handleSelectionChange"
      :header-cell-style="{ 'text-align': 'center' }">
      <el-table-column label="#" type="index" fixed="left" width="40" align="center" />
      <el-table-column type="selection" width="55" fixed="left" align="center" />
      <el-table-column label="设备编号" :show-overflow-tooltip="true" width="135" align="center" prop="sbmCode" />
      <el-table-column label="设备类型" :show-overflow-tooltip="true" width="100" align="center" prop="sbmType">
        <template v-slot="scope">
          <dict-tag :options="deviceTypeList" :value="scope.row.sbmType" />
        </template>
      </el-table-column>
      <el-table-column label="设备名称" :show-overflow-tooltip="true" width="135" prop="sbmName" />
      <el-table-column label="规格型号" :show-overflow-tooltip="true" width="135" prop="sbmGgxh" />
      <el-table-column label="供应厂商" :show-overflow-tooltip="true" width="150" prop="sbmGycs" />
      <el-table-column label="购买日期" :show-overflow-tooltip="true" align="center" prop="sbmCgrq" width="150">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.sbmCgrq, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="联系方式" :show-overflow-tooltip="true" width="150" prop="sbmLxfs" />
      <el-table-column label="责任人员" :show-overflow-tooltip="true" width="100" align="center" prop="sbmZrry" />
      <el-table-column label="设备状态" :show-overflow-tooltip="true" width="100" align="center" prop="sbmSbzt">
        <template v-slot="scope">
          <dict-tag :options="deviceCondition" :value="scope.row.sbmSbzt" />
        </template>
      </el-table-column>
      <el-table-column label="备注" :show-overflow-tooltip="true" width="100" align="center" prop="sbmDesc" />
      <el-table-column label="建立人员" :show-overflow-tooltip="true" width="100" align="center" prop="sbmJlry" />
      <el-table-column label="建立日期" :show-overflow-tooltip="true" align="center" prop="sbmJlrq" width="150">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.sbmJlrq, '{y}-{m}-{d}  {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更改人员" :show-overflow-tooltip="true" width="100" align="center" prop="sbmGgry" />
      <el-table-column label="更改日期" :show-overflow-tooltip="true" align="center" prop="sbmGgrq" width="150">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.sbmGgrq, '{y}-{m}-{d}  {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" width="120" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['device:assistDevice:edit']">
            修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['device:assistDevice:remove']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 查询对话框   -->
    <el-dialog :title="title" v-model="openSearch" :close-on-click-modal="false" draggable width="400px" append-to-body>
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="88px">
        <el-col :span="24">
          <el-form-item label="设备编号" prop="sbmCode">
            <el-input v-model="queryParams.sbmCode" placeholder="请输入设备编号" clearable size="small" @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="设备类型" prop="sbmType">
            <el-select v-model="queryParams.sbmTypes" multiple placeholder="请选择设备类型" clearable size="small">
              <el-option
                v-for="item in deviceTypeList"
                :key="item.dictValue"
                :label="'[' + item.dictValue + ']' + item.dictLabel"
                :value="item.dictValue" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="设备名称" prop="sbmName">
            <el-input v-model="queryParams.sbmName" placeholder="请输入设备名称" clearable size="small" @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item style="margin-top: 25px; display: flex; justify-content: flex-end">
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>

    <!-- 添加或修改辅助设备对话框 -->
    <el-dialog :title="title" v-model="open" :close-on-click-modal="false" draggable width="750px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-col :span="12">
          <el-form-item label="设备类型" prop="sbmType">
            <el-select v-model="form.sbmType" class="input240" placeholder="请选择设备类型">
              <el-option
                v-for="item in deviceTypeEfficient"
                :key="item.dictValue"
                :label="'[' + item.dictValue + ']' + item.dictLabel"
                :value="item.dictValue" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备名称" prop="sbmName">
            <el-input v-model="form.sbmName" placeholder="请输入设备名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="规格型号" prop="sbmGgxh">
            <el-input v-model="form.sbmGgxh" class="input240" placeholder="请输入规格型号" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="供应厂商" prop="sbmGycs">
            <el-input v-model="form.sbmGycs" placeholder="请输入供应厂商" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="购买日期" prop="sbmCgrq">
            <el-date-picker
              clearable
              v-model="form.sbmCgrq"
              type="date"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              placeholder="选择购买日期"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系方式" prop="sbmLxfs">
            <el-input v-model="form.sbmLxfs" placeholder="请输入联系方式" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备状态" prop="sbmSbzt">
            <el-select v-model="form.sbmSbzt" class="input240" placeholder="请选择设备状态">
              <el-option
                v-for="item in deviceEfficientCondition"
                :key="item.dictValue"
                :label="'[' + item.dictValue + ']' + item.dictLabel"
                :value="item.dictValue" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="责任人员" prop="sbmZrry">
            <el-input v-model="form.sbmZrry" placeholder="请输入责任人员" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="sbmDesc">
            <el-input v-model="form.sbmDesc" placeholder="请输入备注" />
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAssistDevice, delAssistDevice, addAssistDevice, updateAssistDevice, getAssistDeviceById } from '@/api/device/assistDevice'
import { getEffectiveDeviceType } from '@/api/device/deviceType'
import { exportTable } from '@/utils/ruoyi'

export default {
  name: 'AssistDevice',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      tableH: document.body.clientHeight - 300,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      // 总条数
      total: 0,
      // 辅助设备表格数据
      assistDeviceList: [],
      // 设备类型
      deviceTypeList: [],
      // 有效的设备类型
      deviceTypeEfficient: [],
      // 设备状态字典
      deviceCondition: [],
      // 有效的设备状态字典
      deviceEfficientCondition: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: document.body.clientHeight > 900 ? 25 : 17,
        sbmCode: null,
        sbmType: null,
        sbmName: null,
        sbmTypes: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sbmType: [{ required: true, message: '设备类型不能为空', trigger: 'blur' }],
        sbmSbzt: [{ required: true, message: '设备状态不能为空', trigger: 'blur' }],
        sbmName: [{ required: true, message: '设备名称不能为空', trigger: 'blur' }],
      },
    }
  },
  created() {
    getEffectiveDeviceType('0').then((res) => {
      this.deviceTypeEfficient = res.data
      for (let item of this.deviceTypeEfficient) {
        item.dictLabel = item.dtypeName
        item.listClass = 'default'
        item.dictValue = item.dtypeCode
      }
    })
    getEffectiveDeviceType().then((res) => {
      this.deviceTypeList = res.data
      for (let item of this.deviceTypeList) {
        item.dictLabel = item.dtypeName
        item.listClass = 'default'
        item.dictValue = item.dtypeCode
      }
    })
    this.getDicts('mould_status').then((response) => {
      this.deviceEfficientCondition = response.data
    })
    this.getAllDicts('mould_status').then((response) => {
      this.deviceCondition = response.data
    })
    this.getList()
  },
  methods: {
    /** 查询辅助设备列表 */
    getList() {
      this.loading = true
      listAssistDevice(this.queryParams).then((response) => {
        this.assistDeviceList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    // 取消按钮
    cancel() {
      this.open = false
      this.openSearch = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        sbmId: null,
        sbmCode: null,
        sbmType: null,
        sbmTypes: null,
        sbmName: null,
        sbmGgxh: null,
        sbmGycs: null,
        sbmCgrq: null,
        sbmLxfs: null,
        sbmZrry: null,
        sbmDesc: null,
        sbmSbzt: null,
        sbmJlry: null,
        sbmJlrq: null,
        sbmGgry: null,
        sbmGgrq: null,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.assistDeviceList = []
      this.getList()
      this.openSearch = false
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.sbmId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加辅助设备'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const sbmId = row.sbmId || this.ids
      getAssistDeviceById(sbmId).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改辅助设备'
      })
    },
    handleSearch() {
      this.reset()
      this.title = '查询辅助设备'
      this.openSearch = true
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.sbmId != null) {
            updateAssistDevice(this.form).then((response) => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addAssistDevice(this.form).then((response) => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const sbmIds = row.sbmId || this.ids
      this.$confirm('是否确认删除辅助设备编号为"' + sbmIds + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(function () {
          return delAssistDevice(sbmIds)
        })
        .then(() => {
          this.msgSuccess('删除成功')
          this.getList()
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport(a, b) {
      exportTable(a, b)
    },
    /** 分页 **/
    pageChange(page) {
      this.queryParams.pageNum = page
      this.getList()
    },
  },
}
</script>

<style lang="scss" scoped></style>
